<?php
// $Header: /cvsroot/tsheet/timesheet.php/admin_report_all.php,v 1.5 2005/05/23 10:42:46 vexil Exp $

// Authenticate
require("class.AuthenticationManager.php");
require("class.CommandMenu.php");
if (!$authenticationManager->isLoggedIn() || !$authenticationManager->hasClearance(CLEARANCE_ADMINISTRATOR)) 
{
	Header("Location: login.php?redirect=$_SERVER[PHP_SELF]&clearanceRequired=Administrator");
	exit;
}

// Connect to database.
$dbh         = dbConnect();
$contextUser = strtolower($_SESSION['contextUser']);

// Load local vars from superglobals
$uid     = isset($_REQUEST["uid"]) ? $_REQUEST["uid"]: 1;
$orderby = isset($_REQUEST["orderby"]) ? $_REQUEST["orderby"]: "username";
	
//define the command menu
include("timesheet_menu.inc");


//get the first CTO from the database
$cto = getFirstCTO();



  
// Calculate the previous month.
$next_month = $month + 1;
$next_year  = $year;
$prev_month = $month - 1;
$prev_year  = $year;

//rollover year forward
if (!checkdate($next_month, 1, $next_year)) {
	$next_month -= 12;
	$next_year ++;
}

//rollover year back
if (!checkdate($prev_month, 1, $prev_year)) {
    $prev_month += 12;
    $prev_year --;
}

?>
<html>
    <head>
        <title>Timesheet.php Report: All days this month</title>
        <?php include ("header.inc"); ?>
    </head>
    <body <?php include ("body.inc"); ?> >
        <?php include ("banner.inc"); ?>
    
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
        	<tr>
        		<td width="100%" class="face_padding_cell">
        			<table width="100%" border="0">
        				<tr>
						<td align="left" nowrap>
                                <table width="100%" height="100%" border="0" cellpadding="1" cellspacing="2">
                                    <tr>
                                        <td align="right" width="0" class="outer_table_heading">
										    CTO:
										</td>
                                        <td align="left" width="100%">
                                            <?php cto_select_droplist($cto, false); ?>
                                        </td>
                                    </tr>
                                </table>
                            </td>
        					<td align="left" nowrap class="outer_table_heading">
        					    <?php echo date('F Y',mktime(0,0,0,$month,1,$year)) ?>
        					    </td>
        					<td align="right" nowrap>
        					    <?php print "<a href=\"$_SERVER[PHP_SELF]?uid=$uid&cto=$cto&month=$prev_month&year=$prev_year\" class=\"outer_table_action\">Prev</a>&nbsp;";
        					    print "<a HREF=\"$_SERVER[PHP_SELF]?uid=$uid&cto=$cto&month=$next_month&year=$next_year\" class=\"outer_table_action\">Next</a>"; ?>
        					</td>
        				</tr>
        			</table>
        	        <table width="100%" align="center" border="0" cellpadding="0" cellspacing="0" class="outer_table">
        	        	<tr>
        	        		<td>			
        	        			<table width="100%" border="0" cellpadding="0" cellspacing="0" class="table_body">
        	        				<tr class="inner_table_head">
        	        					<td class="inner_table_column_heading">Name</td>
        	        					<td class="inner_table_column_heading">Days</td>
        	        					<td class="inner_table_column_heading">
        	        						<b><a href="<?php echo $_SERVER["PHP_SELF"]; ?>?orderby=<?php echo $CLIENT_TABLE; ?>.organisation&month=<?php echo $month; ?>&year=<?php echo $year; ?>" class="inner_table_column_heading">Client</a></b>
        	        					</td>
										<td class="inner_table_column_heading">
        	        						<b><a href="<?php echo $_SERVER["PHP_SELF"]; ?>?orderby=<?php echo $PROJECT_TABLE; ?>.proj_id&month=<?php echo $month; ?>&year=<?php echo $year; ?>" class="inner_table_column_heading">Project</a></b>
        	        					</td>
        	        					<td class="inner_table_column_heading">
        	        						<b><a href="<?php echo $_SERVER["PHP_SELF"]; ?>?orderby=<?php echo $TASK_TABLE; ?>.task_id&month=<?php echo $month; ?>&year=<?php echo $year; ?>" class="inner_table_column_heading">Task</a></b>
        	        					</td>	
                                        <td class="inner_table_column_heading">Task Manager</td>										
        	        				</tr>				
        <?php 
        	$query = " select   distinct first_name, last_name, $USER_TABLE.username, $PROJECT_TABLE.title, $PROJECT_TABLE.proj_id, $TASK_TABLE.name, $TASK_TABLE.manager, $TASK_TABLE.task_id, $CLIENT_TABLE.organisation, $USER_TABLE.cto ".
        		     " FROM      $USER_TABLE, $PROJECT_TABLE, $TASK_TABLE, $ASSIGNMENTS_TABLE, $TASK_ASSIGNMENTS_TABLE, $CLIENT_TABLE ".
					 " WHERE     $ASSIGNMENTS_TABLE.proj_id = $PROJECT_TABLE.proj_id and $TASK_ASSIGNMENTS_TABLE.task_id = $TASK_TABLE.task_id and $CLIENT_TABLE.client_id = $PROJECT_TABLE.client_id AND $USER_TABLE.cto='$cto' ".
        		     " AND       $PROJECT_TABLE.title = $TASK_TABLE.source_project AND ".
        			            "$PROJECT_TABLE.title = $TASK_TABLE.target_project AND ".
                                "$ASSIGNMENTS_TABLE.username = $USER_TABLE.username and $TASK_ASSIGNMENTS_TABLE.username = $USER_TABLE.username AND $USER_TABLE.username NOT IN ('admin','guest') ORDER BY $orderby, $CLIENT_TABLE.organisation, $PROJECT_TABLE.title, $TASK_TABLE.name  ";
        
        	list ($qh,$num) = dbQuery($query);
        	$last_username = "";
        
        	if ($num == 0) {
        		print "	<tr>\n";
        		print "		<td align=\"center\">\n";
        		print "			<i><br>No Days recorded.<br><br></i>\n";
        		print "		</td>\n";
        		print "	</tr>\n";
        	}
        	else {
        		while ($name_data = dbResult($qh)) {
        			$queryBis = "SELECT sec_to_time(sum(unix_timestamp(end_time) - unix_timestamp(start_time))) AS diff " .
        				     "FROM $TIMES_TABLE WHERE ".
                             "start_time >= '$year-$month-1' AND end_time < '$next_year-$next_month-1' and end_time > 0 ".
                             "and uid='$name_data[username]' AND task_id=$name_data[task_id] and proj_id=$name_data[proj_id]";
                    
			  
			  
        			list($qh2, $num2) = dbQuery($queryBis);
        			if ($num2 > 0)
        			{
        				$time_data = dbResult($qh2);
                    }
					
				if ($num2 > 0 && isset($time_data["diff"]))
        		{
					
					
					
					
        			print "<tr>\n";
        			if ($last_username != $name_data["username"]) 
        			{
        				$last_username = $name_data["username"];
        				print "<td class=\"calendar_cell_middle\">$name_data[first_name] $name_data[last_name]</TD>\n";
        			}
        			else 
        			{
        				print "<td class=\"calendar_cell_middle\">&nbsp;</td>\n";
        			}
        
		
		
        			print "<td class=\"calendar_cell_middle\" align=\"center\">";
        			if ($num2 > 0 && isset($time_data["diff"]))
        			{
        			    $duration = $time_data["diff"] /8;
        				echo $duration;
        			}
        			else
        			{
            			print "&nbsp;";
        			}
        			print "</td>\n\n";
					
					
					
					$manager      = $name_data["manager"];
                    $client       = stripslashes($name_data["organisation"]);
                    $projectTitle = stripslashes($name_data["title"]);
                    $taskName     = stripslashes($name_data["name"]);
        			print "<td class=\"calendar_cell_disabled_right\"><a href=\"javascript:void(0)\" ONCLICK=window.open(\"proj_info.php?proj_id=$name_data[proj_id]\",\"Info\",\"location=0,directories=no,status=no,menubar=no,resizable=1,scrollbar=yes,width=580,height=200\") class=\"outer_table_action\">$client</A></TD>\n";
        			print "<td class=\"calendar_cell_disabled_right\"><a href=\"javascript:void(0)\" ONCLICK=window.open(\"proj_info.php?proj_id=$name_data[proj_id]\",\"Info\",\"location=0,directories=no,status=no,menubar=no,resizable=1,scrollbar=yes,width=580,height=200\") class=\"outer_table_action\">$projectTitle</A></TD>\n";
        			print "<td class=\"calendar_cell_disabled_right\"><a href=\"javascript:void(0)\" ONCLICK=window.open(\"task_info.php?proj_id=$name_data[proj_id]&task_id=$name_data[task_id]\",\"TaskInfo\",\"location=0,directories=no,status=no,scrollbar=yes,menubar=no,resizable=1,width=580,height=220\")>$taskName</A></TD>\n";
        	        print "<td class=\"calendar_cell_disabled_right\">$manager</TD>\n";
        	        print "</tr>\n";
				}
        	}
        }
        ?>
        	        			</TABLE>
        	        		</td>
        	        	</tr>
        	        </table>
        		</td>
        	</tr>
        </table>
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
            	<td align="center" nowrap>
            	   <a href="monthly_imputation_download_xls.php?month=<?php print $month; ?>&year=<?php print $year; ?>&query=<?php print $query; ?>&mode=monthly">
        		       <input name="downloadButton" type="button" id="downloadButton" value="Download" onClick="DownloadMonthlyImputation()"> </input> 
        		   </a>
        		   <br><br>
            	</td>
            </tr>
        </table> 
        <?php include ("footer.inc");?>
    </BODY>
</HTML>
